﻿package com.zit.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zit.dao.TeacherDAO;
import com.zit.entity.Classes;
import com.zit.entity.Dept;
import com.zit.entity.Teacher;



/**
 * 教师管理Service
 * 
 * @author 唐美兰
 * @createDate 2013-12-03
 */
public class TeacherService extends BaseService {

	TeacherDAO td = new TeacherDAO();

	/**
	 * 查询所有部门
	 * 
	 * @param s
	 * @return
	 */
	public List<Dept> selectdept() {
		Session s = null;
		Transaction tx = null;
		List<Dept> tc = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			tc = td.selectdept(s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
		return tc;
	}

	/**
	 * 根据部门查询部门下面的教师
	 * 
	 * @param id
	 * @param s
	 * @return
	 */
	public List<Teacher> selectteacher(Integer id) {
		Session s = null;
		Transaction tx = null;
		List<Teacher> tc = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			tc = td.selectteacherid(id, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
		return tc;
	}

	/**
	 * 查询所有老师
	 * 
	 * @return
	 */
	public List<Teacher> selectteacher() {
		Session s = null;
		Transaction tx = null;
		List<Teacher> tc = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			tc = td.selectteacher(s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
		return tc;
	}
	
	/**
	 * 根据id查询老师
	 * 
	 * @return
	 */
	public Teacher idselectteacher(Integer id) {
		Session s = null;
		Transaction tx = null;
		Teacher tc = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			tc = td.idselectteacher(s, id);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
		return tc;
	}

	/**
	 * 修改老师信息
	 * 
	 * @param id
	 */
	public void updateteacherdept(Teacher teacher) {
		Session s = null;
		Transaction tx = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			td.updateteacherdept(teacher, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
	}

	/**
	 * 修改老师的部门
	 * 
	 * @param id
	 */
	public void updateteacherdept(Integer deptid, Integer id) {
		Session s = null;
		Transaction tx = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			td.updateteacherdept(deptid, id, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
	}

	/**
	 * 根据id删除老师信息
	 * 
	 * @param id
	 */
	public void deleteteacher(Integer id) {
		Session s = null;
		Transaction tx = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			td.deleteteacher(id, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
	}
	private TeacherDAO teacherDAO = new TeacherDAO();
	
	/**
	 * 功能:用户登录
	 * 作者:王冲
	 * @param teacher
	 * @return
	 */
	public Teacher login(Teacher teacher){
		Session s = null;
		Transaction tx = null ;
		Teacher t=null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			t=teacherDAO.login(teacher, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			s.close();
		}
		return t;
	}
	
	public int countClasses(Integer tid) {
		Session s = null;
		Transaction tx = null ;
		int t = 0;
		try {
			s = getSession();
			tx = s.beginTransaction();
			//查询当前教师
			Teacher thisTeacher = (Teacher) baseDAO.findById(Teacher.class, tid, s);
			//获取当前教师的部门
			Dept dept = thisTeacher.getDept();
			t=teacherDAO.countClasses(tid,dept.getId(), s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			s.close();
		}
		return t;
	}

	public Teacher findByteaching(Integer id) {
		Session s = null;
		Transaction tx = null ;
		Teacher t=null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			t=teacherDAO.findByteaching(id, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			s.close();
		}
		return t;
	}

	public List<Teacher> findChangeTeaForDelTeacher(Teacher teacher) {
		Session s = null;
		Transaction tx = null;
		List<Teacher> tc = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			tc = td.findChangeTeaForDelTeacher(teacher, s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
		return tc;
	}

	public void deleteteacher(Teacher teacher, int[] classesids, int[] teacherids) {
		Session s = null;
		Transaction tx = null;
		try {
			s = getSession();
			tx = s.beginTransaction();
			if(teacher.getIsPro()==0){
				for (int i=0 ; i<classesids.length ; i++) {
					Classes c = (Classes) teacherDAO.findById(Classes.class, classesids[i], s);
					c.setTeacherByProTeacher((Teacher)teacherDAO.findById(Teacher.class, teacherids[i], s));
					teacherDAO.update(c, s);
				}
			}else{
				for (int i=0 ; i<classesids.length ; i++) {
					Classes c = (Classes) teacherDAO.findById(Classes.class, classesids[i], s);
					c.setTeacherByHeadTeacher((Teacher)teacherDAO.findById(Teacher.class, teacherids[i], s));
					teacherDAO.update(c, s);
				}
			}
			teacherDAO.deleteteacher(teacher.getId(), s);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			s.close();
		}
	}
}
